package com.bos.service.impl;

import com.bos.dao.repositories.CourierRepository;
import com.bos.domian.base.Courier;
import com.bos.domian.base.FixedArea;
import com.bos.service.CourierService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.criteria.*;
import java.util.*;

/**
 * @author misterWei
 * @create 2018年11月13号:09点33分
 * @mailbox mynameisweiyan@gmail.com
 */
@Transactional
@Service
public class CourierServiceImpl implements CourierService {
    @Autowired
    private CourierRepository courierRepository;

    @Override
    public void save(Courier courier) {
        courierRepository.save(courier);
    }

    @Override
    public Map<String, Object> pageQuery(Integer page, Integer rows, Courier courier) {
        Page<Courier> all = null;
        if (courier != null) {
            Specification<Courier> specification = new Specification<Courier>() {
                @Override
                public Predicate toPredicate(Root<Courier> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                    Predicate and = null;
                    if (StringUtils.isNotBlank(courier.getCourierNum())) {
                        Predicate courierNum = cb.equal(root.get("courierNum").as(String.class), courier.getCourierNum());
                        and = cb.and(courierNum);
                    }
                    if (StringUtils.isNotBlank(courier.getCompany())) {
                        Predicate company = cb.like(root.get("company").as(String.class), "%" + courier.getCompany() + "%");
                        and = cb.and(company);
                    }
                    if (StringUtils.isNotBlank(courier.getType())) {
                        Predicate type = cb.like(root.get("type").as(String.class), "%" + courier.getType() + "%");
                        and = cb.and(type);
                    }
                    //多表查询
                    if (courier.getStandard() != null) {
                        if (StringUtils.isNotBlank(courier.getStandard().getName())) {
                            //多表查询
                            Join<Object, Object> standard = root.join("standard", JoinType.INNER);
                            Predicate name = cb.like(standard.get("name").as(String.class), courier.getStandard().getName());
                            and = cb.and(name);
                        }

                    }
                    return and;
                }
            };
            all = courierRepository.findAll(specification, new PageRequest(page, rows));
        } else {
            all = courierRepository.findAll(new PageRequest(page, rows));
        }

        Map<String, Object> courierList = new HashMap<>();
        courierList.put("total", all.getTotalElements());
        courierList.put("rows", all.getContent());
        return courierList;
    }

    @Override
    public void delAndstart(String ids, Boolean flag) {
        String[] disStrArr = ids.split(",");
        for (String s : disStrArr) {
            Integer id = Integer.parseInt(s);
            if (flag) {
                //手动编写的HQML语句  一个语句实现还原，作废功能。
                courierRepository.delBatchOrStart(id, '0');
            } else {
                courierRepository.delBatchOrStart(id, '1');
            }

        }

    }


    @Override
    public List<Courier> noassociationCourier() {
        //查询没有定区关联的快递员
        Specification specification = new Specification() {

            @Override
            public Predicate toPredicate(Root root, CriteriaQuery query, CriteriaBuilder cb) {
                //查询分区为空的快递员
                Predicate fixedAreas = cb.isEmpty(root.get("fixedAreas").as(Set.class));
                return fixedAreas;
            }
        };
        List<Courier> courierList = courierRepository.findAll(specification);
        return courierList;
    }
}
